<?php

//创建websocket服务器对象，监听0.0.0.0:50271端口
$ws = new Swoole\WebSocket\Server("0.0.0.0", 50271);

//监听WebSocket连接打开事件
$ws->on('open', function ($ws, $request) {
	echo "客户端{$request->fd}上线\n";
});

//监听WebSocket消息事件
$ws->on('message', function ($ws, $frame) {
    echo "客户端{$frame->fd}说:{$frame->data}\n";
    echo "我说:你好客户端";
    $ws->push($frame->fd, "你好客户端");
});

//监听WebSocket连接关闭事件
$ws->on('close', function ($ws, $fd) {
    echo "客户端{$fd}下线\n";
});

$ws->start();
